# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4.QtGui import *
from PyQt4 import QtCore

from ui_reactionDetails import Ui_ReactionDetailsFrame

pressureConversions = {"atm":1, "Torr":760, "kPa":101.325}

class ReactionDetailsWidget(QScrollArea):

	def __init__(self):
		QWidget.__init__(self)

		# Set up the user interface from Designer.
		self.ui = Ui_ReactionDetailsFrame()
		self.ui.setupUi(self)

		self.reaction = None

		self.ui.pressureUnitsComboBox.addItems(["atm", "Torr", "kPa"])
		self.units = "atm"

		self.connect(self.ui.pressureUnitsComboBox, QtCore.SIGNAL("currentIndexChanged(const QString &)"), self.pressureUnitChanged)


	def setReaction(self, reaction):
		self.clear()
		self.reaction = reaction
		
		for react in self.reaction.reactants:
			s = react.name
			if react.percent:
				s += "\t%i%% by mol" % (react.percent)
			else:
				s += "\tUnknown% by mol"
			self.ui.reactantList.addItem(s)

		for prod in self.reaction.products:
			s = prod.name
			if prod.percent:
				s += "\t%i%% yield" % (prod.percent)
			else:
				s += "\tUnknown% yield"
			self.ui.productList.addItem(s)

		self.ui.catalystList.addItems([i.name for i in self.reaction.catalysts])

		if self.reaction.reference:
			self.ui.referenceLineEdit.setText(self.reaction.reference.title)
			self.ui.refAuthorsLineEdit.setText(self.reaction.reference.author_name)
			self.ui.refYearSpinBox.setValue(self.reaction.reference.year)
		if self.reaction.temperature:
			self.ui.temperatureSpinBox.setValue(self.reaction.temperature)
		if self.reaction.pressure:
			self.ui.pressureSpinBox.setValue(self.reaction.pressure)
			self.ui.pressureUnitsComboBox.setCurrentIndex(0)
		if self.reaction.time:
			self.ui.daysSpinBox.setValue(int(self.reaction.time/60/24))
			self.ui.hoursSpinBox.setValue(int(self.reaction.time/60)%24)
			self.ui.minutesSpinBox.setValue(int(self.reaction.time%60))
			self.ui.secondsSpinBox.setValue((self.reaction.time-int(self.reaction.time))*60)
		if self.reaction.pH:
			self.ui.pHSpinBox.setValue(self.reaction.pH)
		if self.reaction.solvent:
			self.ui.solventLineEdit.setText(self.reaction.solvent)
		if self.reaction.solid:
			self.ui.solidCheckBox.setChecked(True)
		if self.reaction.liquid:
			self.ui.liquidCheckBox.setChecked(True)
		if self.reaction.gas:
			self.ui.gasCheckBox.setChecked(True)
		if self.reaction.other:
			self.ui.otherLineEdit.setText(self.reaction.other)

	def clear(self):
		self.ui.reactantList.clear()
		self.ui.productList.clear()
		self.ui.catalystList.clear()

		self.ui.referenceLineEdit.clear()
		self.ui.refAuthorsLineEdit.clear()
		self.ui.refYearSpinBox.setValue(1800)
		self.ui.temperatureSpinBox.setValue(22)
		self.ui.pressureSpinBox.setValue(1)
		self.ui.pressureUnitsComboBox.setCurrentIndex(0)
		self.units = "atm"
		self.ui.daysSpinBox.setValue(0)
		self.ui.hoursSpinBox.setValue(0)
		self.ui.minutesSpinBox.setValue(0)
		self.ui.secondsSpinBox.setValue(0)
		self.ui.pHSpinBox.setValue(7)
		self.ui.solventLineEdit.clear()
		self.ui.solidCheckBox.setChecked(False)
		self.ui.liquidCheckBox.setChecked(False)
		self.ui.gasCheckBox.setChecked(False)
		self.ui.otherLineEdit.clear()


	def pressureUnitChanged(self, units):
		units = str(units)
		pressure = float(self.ui.pressureSpinBox.value()) / pressureConversions[self.units]
		pressure *= pressureConversions[units]
		self.units = units
		self.ui.pressureSpinBox.setValue(pressure)